import java.util.Random;

public class Demo2 {
    public static int sum1 = 0;
    public static int sum2 = 0;
    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        int count = 1000_0000;
        int[] arr = new int[count];
        Random random = new Random();
        Object locker = new Object();
        for (int i = 0; i < count; i++) {
            arr[i] = random.nextInt(100) + 1;
        }

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < count; i+=2) {
                sum1 += arr[i];
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 1; i < count; i+=2) {
                sum2 += arr[i];
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        // 记录结束时间
        long end = System.currentTimeMillis();
        // 结果
        System.out.println("总耗时 " + (end - start) + "ms.");
        System.out.println("sum = " + (sum1 + sum2));
    }
}